import 'package:flutter/material.dart';
import 'package:healthy_station_flutter/utils/app_utils.dart';
import 'package:healthy_station_flutter/utils/constants.dart';
import 'package:healthy_station_flutter/utils/res_images.dart';
import '../../nav/nav_route.dart';
import '../../network/XHttp.dart';
import '../../network/api.dart';
import '../../utils/log_utils.dart';
import '../../utils/res_colors.dart';
import '../../utils/res_strings.dart';
import '../../utils/screen_adapter.dart';
import '../../widget/web_view.dart';

/// 
/// @Description 
/// @Author WuJianFeng
/// @Date 2024/3/4 15:06
/// 

class HomeContentWidget extends StatefulWidget{
  const HomeContentWidget({super.key});

  @override
  State<StatefulWidget> createState() => _HomeContentWidgetState();
  
}

class _HomeContentWidgetState extends State<HomeContentWidget>{
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(child: contentTop()),
        contentBottom(),
      ],
    );
  }

  Widget contentTop(){
    return Container(
      margin: EdgeInsets.only(bottom: Adapt.pt(25)),
      child: Stack(
        children: [
          /// 便民医保
          contentItem(Alignment.topLeft,"home_content_doc_query.webp",appCode5),
          /// 智慧家医
          contentItem(Alignment.topCenter,"home_content_doctor.webp",appCode6),
          /// 个人中心
          contentItem(Alignment.topRight,"home_content_center.webp",appCode7),
          /// 预约挂号
          contentItem(Alignment.centerLeft,"home_content_registered.webp",appCode3),
          /// 报告打印
          contentItem(Alignment.center,"home_content_print.webp",appCode4),
          /// 健康查询
          contentItem(Alignment.centerRight,"home_content_query.webp",appCode101),
          /// 一键咨询
          contentItem(Alignment.bottomLeft,"home_content_consult.webp",appCode102),
          /// 复诊开方
          contentItem(Alignment.bottomCenter,"home_content_prescription.webp",appCode103),
          /// 健康体检
          contentItem(Alignment.bottomRight,"home_content_check.webp",appCode104),
        ],
      ),
    );
  }

  Widget contentItem(Alignment alignment, String imgName, String appCode){
    return Positioned(
        child: Align(
          alignment: alignment,
          child: InkWell(
            onTap: (){
              switch(appCode){

                case appCode7 : {
                  if(AppUtils.checkLogin()){
                    Navigator.pushNamed(context, routerPersonalCenter);
                  }
                  break;
                }

                case appCode101 : {
                  break;
                }

                case appCode102 : {
                  break;
                }

                case appCode103 : {
                  break;
                }

                case appCode104 : {
                  break;
                }

                default:{
                  if(AppUtils.checkLogin()){
                    thirdPartyAccess(appCode);
                  }
                  break;
                }
              }
            },
           child: SizedBox(
              width: Adapt.pt(394),
              height: Adapt.pt(227),
              child: ImageUtils.getImg(imgName),
            ),
          ),
        )
    );
  }

  Widget contentBottom(){
    return Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        contentBottomItem("blood_press.webp",sBloodPress),
        contentBottomItem("blood_sugar.webp",sBloodSugar),
        contentBottomItem("body_height.webp",sBodyHeight),
        contentBottomItem("fundus.webp",sFundus),
      ],
    );
  }

  Widget contentBottomItem(String imgName, String slogan){
    return Container(
        decoration:  BoxDecoration(
          gradient:  const LinearGradient(
            begin: FractionalOffset.topCenter,
            end: FractionalOffset.bottomCenter,
            colors: [color_97c3fe, color_3a7afe,],
          ),
          borderRadius:  BorderRadius.circular(15.0),
        ),
        width: Adapt.pt(289),
      height: Adapt.pt(131),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ImageUtils.getImg(imgName),
          Text(slogan, style: TextStyle(fontSize: Adapt.pt(40), color: Colors.white))
        ],
      ),
    );
  }


  void thirdPartyAccess(String appCode){

    XHttp.getInstance().post<String>(Api.thirdPartyAccess, {
      "appCode": appCode,
      "certifcateType": "1",
      "certifcateNo": Constant.cardNumber,
    }).then((value){
      LogUtils.d("__thirdPartyAccess:${value.data}");
      Navigator.of(context).push(MaterialPageRoute(builder: (context) => MyWebView("${value.data}")));

    }).catchError((err) {
      // DO SOMETHING
      LogUtils.d("__developResident-err ${err}");
    });

  }

}
 
 